<%@page import="java.text.DecimalFormat"%>
<%@page import="org.proddevplm.dao.data.conceptSelection.PughAnalysis"%>
<%@page import="org.proddevplm.dao.data.activities.Activity"%>
<%@page import="java.util.List"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.tools.constants.ToolsConstants" %>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>

<%
	List<Activity> activitiesForUser = (List)session.getAttribute("activitiesForUser");
	PughAnalysis pughByActName = (PughAnalysis)session.getAttribute("pughByActName");
%>

<script>

	function loadPughTab(page){
		$("#pughTabContent").load(page);
	}
	
	function loadPughTabWithReq(request, page) {
        $.post(request, function(data) {
            $("#pughTabContent").load(page);
        });
	}
	
	var activityName="";
	function populateActivityName(){
		var name = $("#activitySelect").val();
		activityName = name;
	}
	
	$(function(){
		$("#allPughContent").hide();
		$("#pughTabs").tabs();
		$("#pughTab2").hide();
		$("#pughTab3").hide();
		$("#pughTab4").hide();
		$("button").button();
			
		$("#pughTab1").click(function() {
			loadPughTabWithReq("/proddev/controller?manager=dataAccessManager&method=28&activityName="+activityName,"content/project/devTools/workspace/concepts/defineCriteria.jsp");
		});
		 
		$("#pughTab2").click(function() {
			loadPughTabWithReq("/proddev/controller?manager=dataAccessManager&method=28&activityName="+activityName,"content/project/devTools/workspace/concepts/prioritizeCriteria.jsp");
		});
		 
		$("#pughTab3").click(function() {
			loadPughTabWithReq("/proddev/controller?manager=dataAccessManager&method=28&activityName="+activityName,"content/project/devTools/workspace/concepts/defineConcepts.jsp");
		});
		 
		$("#pughTab4").click(function() {
			loadPughTabWithReq("/proddev/controller?manager=dataAccessManager&method=28&activityName="+activityName,"content/project/devTools/workspace/concepts/pughWorkspace.jsp");
		});
	})
	
	function approveActivity(name){
		$("#activityName").val(name);
		document.forms['approveForm'].submit();
	}
	
	function rejectActivity(name){
		$("#activityName").val(name);
		$("#rejectDialog").dialog("open");
	}
	
	$( "#rejectDialog" ).dialog({
		autoOpen: false,
		height: 350,
		width: 350,
		modal: true,
		buttons: {
			"Propose change": function() {
				var name = document.getElementById("activityName").value;
				var arguments = "activityName="+name+"&"+buildParameter("changeSubject")+"&"+buildParameter("changeDetails");
				ajaxCall("toolsManager", "21", arguments, forward);
				$( this ).dialog( "close" );
				},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "close" );
		}
	});
	
	function forward(data){
		document.forms["rejectForm"].submit();
	}


</script>

<div class="pageTitle">
	<div style="text-align:left;">
		<a href="/proddev/controller?manager=dataAccessManager&method=21">
			<img src="/proddev/images/arrowleft.png" align="middle" height="20" width="30" style="border:0px"></a>
	</div>
	<p style="font-size:small; font-weight:bold; margin-top:-20px">Concept Selection Page</p>

</div>

<%if(pughByActName==null){ %>
	<div style="margin:10 0 0; width:100%">
		<div style="width:30%; float:left; text-align:left; font-weight:bold; color:#555555">
			Please choose the activity you want to complete
		</div>
		<select class="regInput" id="activitySelect" name="activitySelect" onchange="$('#allPughContent').show(), populateActivityName()" size="1" style="width:25%; float:left; margin-left:10px">
			<option SELECTED>-select activity-</option>
			<%if(activitiesForUser!=null){
				for(int i=0; i<activitiesForUser.size(); i++) {
					Activity activity = (Activity)activitiesForUser.get(i);%>
					<option value="<%=activity.getName()%>"><%=activity.getName() %></option>
				<%}
			}
			%>		
		</select>
		<div style="clear:both"></div>
	</div>
	
	
	<div style="clear:both"></div>
	
	<div id="allPughContent" align="center" style="margin-top:10px">
	
		<div id="pughTabs" style="width:auto; font-size:small; ">
		    <ul>
		        <li><a id="pughTab1" href="dummy">1. Define selection criteria</a></li>
		        <li><a id="pughTab2" href="dummy">2. Prioritize selection criteria</a></li>
			   	<li><a id="pughTab3" href="dummy">3. Define concepts</a></li>
			    <li><a id="pughTab4" href="dummy">4. Select concept</a></li>
		    </ul>
	    
		    <div id="pughTabContent" style="margin-top:-10px;"></div>
		    <div style="clear:both"></div>
	
		</div>
		
	</div>
<%}else{%>
<%
	String criteriaString = pughByActName.getCriteria();
	String[] criteria = criteriaString.split("~");
	String conceptsString = pughByActName.getConcepts();
	String[] concepts = conceptsString.split("~");
	String criteriaImportances = pughByActName.getCriteriaImportances();
	String[] importances = criteriaImportances.split("~");
	String matrixValues = pughByActName.getMatrixValues();
	String[] matrix = matrixValues.split("~");
	String[] symbols = new String[matrix.length];
	for(int i=0; i<matrix.length; i++){
		if(Integer.valueOf(matrix[i]) == -3){
			symbols[i] = "--";
		}else if(Integer.valueOf(matrix[i]) == -1){
			symbols[i] = "-";
		}else if(Integer.valueOf(matrix[i]) == 0){
			symbols[i] = "O";
		}else if(Integer.valueOf(matrix[i]) == 1){
			symbols[i] = "+";
		}else if(Integer.valueOf(matrix[i]) == 3){
			symbols[i] = "++";
		}
	}
	
	String[][] pughMatrix = new String[criteria.length][concepts.length];
	
	int l=0; int m=0;
	int cont = 0;
	for(int index=0; index<symbols.length; index++){
		
		String value = symbols[index];
		if(cont==(concepts.length-1)){
			pughMatrix[l][m] = value;
			l++;
			m=0;
			cont=0;
		}else{
			pughMatrix[l][m] = value;
			m++;
			cont++;
		}
	}
%>
	
<div align="center" style="margin-top:10px;">
	<table id="pughTable" border="1" cellspacing="0" cellpadding="3" style="width:60%; font-size:small; ">
		
		<tr id="headerUp" style="height:60px; background-color:#F0F0F6">
			<td style="font-weight:bold">Concepts</td>
			<%for(int i=0; i<concepts.length; i++){%>
				<td rowspan="2" style="padding:0"><span class="rotatedText" style="margin-bottom:0px; margin:-20px">
					<%=concepts[i] %>
					</span>
				</td>
			<%}%>
			<td rowspan="2" style="font-weight:bold;">Importance</td>
		</tr>
		<tr id="headerDown" style="height:60px; background-color:#F0F0F6">
			<td style="font-weight:bold">Criteria</td>
		</tr>
		<%for(int i=0; i<criteria.length; i++){ %>
			<tr>
				<td><%=criteria[i] %></td>
				<%for(int j=0; j<concepts.length; j++){ %>
					<td style="width:10%; text-align:center">
						<%=pughMatrix[i][j] %>
					</td>
				<%} %>
				<td>
					<%Float imp = Float.valueOf(importances[i]);
					Float value = imp*100;
					DecimalFormat dFormat = new DecimalFormat("0.00");
					String shownVal = dFormat.format(value)+"%";%>
					<%=shownVal %>
				</td>
			</tr>
		<%} %>
	</table>
	
	<div id="pughResults" style="width:60%;  margin-top: 20px;">
		<table border="1" cellspacing="0" cellpadding="3" style="width:100%; font-size:small;">
			<tr style="background-color:#F0F0F6">
				<td>&nbsp</td>
				<%for (int k=0; k<concepts.length; k++){%>
					<td><%=concepts[k] %></td>
				<%} %>
			</tr>
			<tr>
				<td style="background-color:#F0F0F6">Positive effects</td>
				<%for(int k=0; k<concepts.length; k++) {%>
					<td >
						<%String pos = pughByActName.getPositiveEffects();
						String[] positives = pos.split("~");
						Float imp = Float.valueOf(positives[k]);
						Float value = imp*100;
						DecimalFormat dFormat = new DecimalFormat("0.00");
						String shownVal = dFormat.format(value)+"%";%>
						<%=shownVal %>
	
					</td>
				<%} %>
			</tr>
			<tr>
				<td style="background-color:#F0F0F6">Negative effects</td>
				<%for(int k=0; k<concepts.length; k++) {%>
					<td >
						<%String neg = pughByActName.getNegativeEffects();
						String[] negatives = neg.split("~");
						Float imp = Float.valueOf(negatives[k]);
						Float value = imp*100;
						DecimalFormat dFormat = new DecimalFormat("0.00");
						String shownVal = dFormat.format(value)+"%";%>
						<%=shownVal %>
					</td>
				<%} %>
			</tr>
			<tr>
				<td style="background-color:#F0F0F6">Net effects</td>
				<%for(int k=0; k<concepts.length; k++) {%>
					<td >
						<%String neut = pughByActName.getConceptResults();
						String[] neutrals = neut.split("~");
						Float imp = Float.valueOf(neutrals[k]);
						Float value = imp*100;
						DecimalFormat dFormat = new DecimalFormat("0.00");
						String shownVal = dFormat.format(value)+"%";%>
						<%=shownVal %>
					</td>
				<%} %>
			</tr>
		</table>
		
		<%String activityName = pughByActName.getActivityName();%>
		<div style="width:100%; margin-top:10px">
			<button id="rejectActivity" name="rejectActivity" onclick="rejectActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:left">Reject activity</button>
			<button id="approveActivity" name="approveActivity" onclick="approveActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:right">Approve activity</button>
			<div style="clear:both"></div>
		</div>
			
		<div id="rejectDialog" title="Propose changes" style="display:none">
			<jsp:include page="/pages/content/project/devTools/workspace/changes/changeReport.jsp"></jsp:include>
		</div>
		
		<form name="rejectForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.TOOLS_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ToolsConstants.STAKEHOLDERS_FWD%>"/>
		</form>
			
		<form name="approveForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.UPDATE_ACTIVITY%>"/>
			<input type="hidden" name="activityName" id="activityName">
		</form>
	</div>
		
</div>
<%}%>